import json
import os

from django.contrib.auth import get_user_model
from django.db import models
from django.utils.translation import gettext_lazy as _


User = get_user_model()


class HostServer(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=_('主键Id'))
    host = models.CharField(max_length=20, null=False, blank=False, verbose_name=_('主机IP'))
    port = models.IntegerField(null=False, blank=False, verbose_name=_('端口'))
    user = models.CharField(max_length=50, null=False, blank=False, verbose_name=_('用户名'))
    password = models.CharField(max_length=50, null=True, blank=True, verbose_name=_('密码'))
    data = models.TextField(null=True, blank=True, verbose_name=_('其他数据'))  # json.dumps(json_data)

    class Meta:
        db_table = 'host_server'
        verbose_name = "服务器"

    def get_comfyui_Installation_path(self):
        json_data = json.loads(self.data)
        comfyui_path = json_data['comfyui_path']
        return comfyui_path

    def get_comfyui_output_path(self):
        comfyui_path = self.get_comfyui_Installation_path()
        output_path = os.path.join(comfyui_path, 'ComfyUI\output')
        return output_path


class WorkflowInfo(models.Model):
    WF_STATUS_CHOICES = (
        (0, _('有效')),
        (1, _('无效')),
    )
    id = models.AutoField(primary_key=True, verbose_name=_('主键Id'))
    workflow = models.TextField(null=True, blank=True, verbose_name=_('文本存储的json工作流'))
    description = models.TextField(null=True, blank=True, verbose_name=_('描述'))
    status = models.IntegerField(choices=WF_STATUS_CHOICES, default=0, verbose_name=_('工作流状态:0-有效, 1-无效'))
    create_date = models.DateTimeField(auto_now_add=True, verbose_name=_('创建时间'))
    update_date = models.DateTimeField(auto_now=True, verbose_name=_('更新时间'))

    class Meta:
        db_table = 'workflow'
        verbose_name = "工作流"

    def __str__(self):
        return self.__class__.__name__


COMFYUI_MODEL_TYPE_LIST = [
    'checkpoints',
    'clip',
    'clip_vision',
    'configs',
    'controlnet',
    'diffusers',
    'diffusion_modelsembeddings',
    'gligen',
    'hypernetworks',
    'loras',
    'photomaker',
    'style_models',
    'text_encoders',
    'unet',
    'upscale_models',
    'vae',
    'vae_approx'
]

class ComfyuiModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=_('主键Id'))
    name = models.CharField(max_length=50, null=False, blank=False, verbose_name=_('名称'))
    type = models.CharField(max_length=50, verbose_name=_('模型类型'))   # COMFYUI_MODEL_TYPE_LIST
    data = models.TextField(null=True, blank=True, verbose_name=_('其他数据'))
    create_date = models.DateTimeField(auto_now_add=True, verbose_name=_('创建时间'))

    def __str__(self):
        return self.__class__.__name__


class ComfyuiClient(models.Model):
    id = models.AutoField(primary_key=True, verbose_name=_('主键Id'))
    client_id = models.CharField(max_length=50, null=False, blank=False, verbose_name=_('名称'))
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_('用户'), related_name='client_user')
    create_date = models.DateTimeField(auto_now_add=True, verbose_name=_('创建时间'))